package Demo6;

import java.util.Objects;

class Money implements Cloneable{
    public double money=12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{//表示改类可以被克隆
    public String name;
    public Money m=new Money();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';

    }
    @Override
    protected Object clone() throws CloneNotSupportedException {//实现深拷贝
        //克隆Student的对象
        Student newstudent=(Student) super.clone();
        //克隆student里的Money对象
        newstudent.m=(Money) this.m.clone();//先克隆，然后把克隆的地址给newstudent
        return newstudent;
        //return super.clone();
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException{
        Student student=new Student();
        student.name="zhangsan";
        Student student1=new Student();
        student1.name="laowu";

        System.out.println(student.equals(student1.name));
        /*
        student.name="zhangsan ";
        Student student2=(Student) student.clone();
        System.out.println(student.m.money);
        System.out.println(student2.m.money);*/
    }
    public static void main9(String[] args) throws CloneNotSupportedException{
        Student student=new Student();
        student.name="zhangsan ";
        Student student2=(Student) student.clone();//浅拷贝
        System.out.println(student);
        System.out.println(student2);
        System.out.println("__________________________");
        student2.name="laoowu";
        System.out.println(student);
        System.out.println(student2);
    }
}
